﻿using SpreadsheetGear;
using Sustainalytics.Entities;
using Sustainalytics.Utils;
using System.Collections.Generic;

namespace Sustainalytics.Portfolios.BL
{
    internal interface IPortfolioSecuritiesPersistance
    {
        void Write(Portfolio portfolioUpload, ICollection<PortfolioSecuritiesRow> rows,
            PortfolioUserClaims userClaims);
    }

    internal class Factory : IPortfolioSecuritiesPersistance
    {
        private readonly List<IPortfolioOutput> _persistanceOutputList;

        private Factory(ICustomPortfolioUpdateHandler manager, IWorksheet worksheet)
        {
            _persistanceOutputList = new List<IPortfolioOutput>
                {
                    new DatabasePersistance(manager),
                    new ExcelPersistance(worksheet)
                };
        }

        public static IPortfolioSecuritiesPersistance GetPortfolioPersistance(ICustomPortfolioUpdateHandler manager,
            IWorksheet worksheet)
        {
            return new Factory(manager, worksheet);
        }

        public void Write(Portfolio portfolioUpload, ICollection<PortfolioSecuritiesRow> rows,
            PortfolioUserClaims userClaims)
        {
            foreach (var persistanceMedia in _persistanceOutputList)
            {
                persistanceMedia.Write(portfolioUpload, rows, userClaims);
            }
        }
    }

    internal class DatabasePersistance : IPortfolioOutput
    {
        private readonly ICustomPortfolioUpdateHandler _manager;

        public DatabasePersistance(ICustomPortfolioUpdateHandler manager)
        {
            _manager = manager;
        }

        public void Write(Portfolio portfolio, ICollection<PortfolioSecuritiesRow> securities,
            PortfolioUserClaims userClaim)
        {
            if (securities == null)
            {
                return;
            }
        }
    }

    internal class ExcelPersistance : IPortfolioOutput
    {
        private readonly IWorksheet _worksheet;
        private static int _row;

        public ExcelPersistance(IWorksheet worksheet)
        {
            _worksheet = worksheet;
            _row = 1;
            AddHeaderForMatchingInfoToFile(worksheet);
        }

        private static int ColumnDetailsOffset
        {
            get { return 4; }
        }

        private static int ColumnCompanyIQIdOffset
        {
            get { return 3; }
        }

        private static int ColumnCompanyNameOffset
        {
            get { return 2; }
        }

        private static void AddWorksheetHeaderCell(IWorksheet worksheetPortfolio, int columnIndex, string text)
        {
            worksheetPortfolio.Cells[0, columnIndex].Value = text;
            worksheetPortfolio.Cells[0, columnIndex].Cells.Interior.Color = Color.FromArgb(0, 106, 158);
            worksheetPortfolio.Cells[0, columnIndex].Font.Bold = true;
            worksheetPortfolio.Cells[0, columnIndex].Font.Name = "Calibri";
            worksheetPortfolio.Cells[0, columnIndex].Font.Size = 10;
            worksheetPortfolio.Cells[0, columnIndex].Font.Color = Color.FromArgb(255, 255, 255);
        }

        private static void AddHeaderForMatchingInfoToFile(IWorksheet worksheetPortfolio)
        {
            AddWorksheetHeaderCell(worksheetPortfolio, ColumnCompanyNameOffset, "Matched Company name");
            AddWorksheetHeaderCell(worksheetPortfolio, ColumnCompanyIQIdOffset, "Matched Company ID");
            AddWorksheetHeaderCell(worksheetPortfolio, ColumnDetailsOffset, "Details");
        }

        public void Write(Portfolio portfolio, ICollection<PortfolioSecuritiesRow> securities,
            PortfolioUserClaims userClaim)
        {
            foreach (var security in securities)
            {
                if (security.IsValid)
                {
                    _worksheet.Cells[_row, ColumnCompanyIQIdOffset].Value =
                        security.Security.MatchedInfo.CapitalIQId;
                    _worksheet.Cells[_row, ColumnCompanyNameOffset].Value =
                        security.Security.MatchedInfo.CompanyName;
                }
                _worksheet.Cells[_row, ColumnDetailsOffset].Value = security.MatchingDetails.IsNullOrEmpty()
                    ? security.Security.AccessDetails
                    : security.MatchingDetails;

                _row++;
            }
        }
    }
}